package org.reliablesource.prism.gef.editor.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.tools.DirectEditManager;
import org.reliablesource.prism.gef.editor.figures.StickyNoteFigure;
import org.reliablesource.prism.gef.editor.model.StickyNoteAdapter;

/**
 * 
 * StickyNoteDirectEditPolicy.java<br>
 * Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved<br><br>
 * 
 * Created on: Apr 12, 2006<br>
 * 
 * @author Dennis Park 
 *      <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *      
 *	@since 1.0
 */
public class StickyNoteEditPart extends AbstractGraphicalEditPart implements
		PropertyChangeListener {

	private DirectEditManager manager;

	/**
	 * Upon activation, attach to the model element as a property change
	 * listener.
	 */
	public void activate() {
		if (!isActive()) {
			super.activate();
			( getCastedModel()).getAnnotation()
					.addPropertyChangeListener(this);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		// allow removal of the associated model element
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new StickyNoteComponentEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
				new StickyNoteDirectEditPolicy());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		IFigure f = createFigureForModel();
		return f;
	}

	/**
	 * Return a IFigure depending on the instance of the current model element.
	 * This allows this EditPart to be used for both sublasses of Shape.
	 */
	private IFigure createFigureForModel() {
		StickyNoteFigure f = new StickyNoteFigure();
		f.setOpaque(false);
		f.setBackgroundColor(ColorConstants.yellow);
		return f;
	}

	/**
	 * Upon deactivation, detach from the model element as a property change
	 * listener.
	 */
	public void deactivate() {
		if (isActive()) {
			super.deactivate();
			((StickyNoteAdapter) getModel()).getAnnotation()
					.removePropertyChangeListener(this);
		}
	}

	private StickyNoteAdapter getCastedModel() {
		return (StickyNoteAdapter) getModel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(final PropertyChangeEvent evt) {
		refreshVisuals();
	}

	protected void refreshVisuals() {
		// notify parent container of changed position & location
		// if this line is removed, the XYLayoutManager used by the parent
		// container
		// will not know the bounds of this figure and will not draw it
		// correctly.
		Rectangle bounds = new Rectangle(getCastedModel().getLocation(),
				getCastedModel().getSize());
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), bounds);
		super.refreshVisuals();
	}

	private void performDirectEdit() {
		if (manager == null)
			manager = new StickyNoteLabelEditManager(this,
					new StickyNoteCellEditorLocator(
							(StickyNoteFigure) getFigure()));
		manager.show();
	}

	public void performRequest(final Request request) {
		if (request.getType() == RequestConstants.REQ_DIRECT_EDIT)
			performDirectEdit();
	}

}